﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RedHouse.AdminControl.UserControls;
using RedHouse.Data.Service;
using RedHouse.Data.Base;
using System.Web.Services;
using System.Web.Script.Serialization;
using RedHouse.Data.Entities;

namespace RedHouse.AdminControl
{
    public partial class Dish : System.Web.UI.Page
    {
        public int Action { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["Action"] != null)
                {
                    Action = int.Parse(Request.QueryString["Action"].ToString());
                }
                Initialize();
            }
        }

        private void Initialize()
        {

            switch (Action)
            {
                case 1:
                    DishDetail.Visible = true;
                    DishList.Visible = false;
                    break;
                case 2:
                    DishDetail.Visible = false;
                    DishList.Visible = true;
                    break;
                default:
                    break;
            }
        }

        [WebMethod]
        public static void DeleteDish(string id)
        {
            Guid dishId = new Guid(id);
            IDishService dishService = new DishService();
            dishService.DeleteDish(dishId);
        }

        [WebMethod()]
        public static string EditDish(string id)
        {
            Guid dishId = new Guid(id);
            IDishService dishService = new DishService();
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Serialize(dishService.GetDishById(dishId));
        }

        [WebMethod()]
        public static string SaveDish(object dish)
        {
            try
            {
                IDishService dishService = new DishService();
                RedHouseDish redHouseDish = GetRedHouseDish(dish);
                dishService.UpdateDish(redHouseDish);
                return "save successfully!";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public static RedHouseDish GetRedHouseDish(object dish)
        {
            RedHouseDish retDish = new RedHouseDish();
            Dictionary<string, object> tmp = (Dictionary<string, object>)dish;
            object id = null;
            object location = null;
            object category = null;
            object lang = null;
            object des = null;
            object imageUrl = null;
            object title = null;
            object order = null;

            tmp.TryGetValue("Id", out id);
            tmp.TryGetValue("Location", out location);
            tmp.TryGetValue("Category", out category);
            tmp.TryGetValue("LangId", out lang);
            tmp.TryGetValue("Description", out des);
            tmp.TryGetValue("ImageUrl", out imageUrl);
            tmp.TryGetValue("Title", out title);
            tmp.TryGetValue("DisplayOrder", out order);

            retDish.Title = title.ToString();
            retDish.Id = new Guid(id.ToString());
            retDish.LocationID = int.Parse(location.ToString());
            retDish.CategoryID = new Guid(category.ToString());
            retDish.LangId = new Guid(lang.ToString());
            retDish.Description = des.ToString();
            retDish.ImageUrl = imageUrl.ToString();
            retDish.CreatedDate = DateTime.Now;
            retDish.CreatedBy = "admin";
            retDish.DisplayOrder = int.Parse(order.ToString());
            return retDish;
        }
    }
}

